package com.cabd.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.cabd.staticfinal.PythonFilePath;
/**
 * @ClassName: HandleDataWithPythonImpl 
 * @Description: TODO(Java执行Python脚本) 
 * @author 詹金浩 
 * @date 2018年3月13日 下午6:22:27
 */
public class HandleDataWithPythonImpl implements com.cabd.service.HandleDataWithPython {

	//采用Runtime.getRuntime().exec(String []args)的方法来执行外部的Python脚本（开了一个进程）。
	//不能使用Jython，因为Jython不能执行Python的第三方库。Python脚本中的所以路径必须使用绝对路径，否则会有FileNotFoundError
	@Override
	public void creatResultFile1(String folderName) throws IOException, InterruptedException {
		 System.err.println("Start Python 1");
		 String [] arg_ = new String[]{"python", PythonFilePath.PythonFilePath_FLIGHT, folderName};
		 //执行外部的Python脚本并且使当前进程等待
		 Process process = Runtime.getRuntime().exec(arg_);
		 //获得Python脚本的输出流
		 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));  
		 String line;
		 while ((line = in.readLine()) != null) {
				System.out.println(line);
	     }
	     in.close();    
	     //当前进程等待
	     process.waitFor();  
	     System.err.println("End Python 1"); 
	}
	public void creatResultFile2(String folderName) throws IOException, InterruptedException {
		
		System.err.println("Start Python 2");
		String []arg = new String[] {"python", PythonFilePath.PythonFilePath_SK, folderName};
		Process process = Runtime.getRuntime().exec(arg);
		BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));  
		String line;
		while ((line = in.readLine()) != null) {
			System.out.println(line);    
	    }
	    in.close();    
	    process.waitFor();  
		System.err.println("Start Python 2");
		
	}

}
